package com.agent.commons.intercept;

import com.agent.commons.Result.Result;
import com.agent.commons.errorcode.GlobalErrorCodeConstants;
import com.agent.commons.exception.BaseException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import static com.agent.commons.errorcode.GlobalErrorCodeConstants.*;

/**
 * 全局异常处理
 */
@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 统一处理自定义异常
     * @param e
     * @return
     */
    @ExceptionHandler(BaseException.class)
    public Result<?> baseExceptionHandler(BaseException e) {
        log.error("baseExceptionHandler:{}", e.getMsg());
        return Result.error(e.getCode(), e.getMsg());
    }

    /**
     * 统一处理所有异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public Result<?> exceptionHandler(Exception e) {
        log.error("exceptionHandler:{}", e.getMessage());
        return Result.error(INTERNAL_SERVER_ERROR.getCode(), "系统错误");
    }
}
